#ifndef __CLIENT_REQUEST_CONNECT_included
#define __CLIENT_REQUEST_CONNECT_included

#include "ClientRequest.h"
#include "Connection.h"
#include "ConnectionFactory.h"
#include "ConnectionControl.h"
#include "../engine/DebugStream.h"

using namespace Engine;

class CRequestConnect : public CClientRequest {

public :

	CRequestConnect(string& peer) :
		CClientRequest(string(COMMAND_CONNECT))
	{
		AddParameter(string("peer"), peer);
	}

	CRequestConnect(boost::asio::streambuf& data) :
		CClientRequest(string(COMMAND_CONNECT), data)
	{
	}

	virtual void DoRequest() {

		Response(RESPONSE_STATUS_OK);

		string& server = GetParameter(string("server"));

		boost::shared_ptr<CConnection> c = 
			CConnectionFactory::CreateByServerDesc(server, boost::dynamic_pointer_cast<CConnectionControl, CConnection>(mConnection), true);
		
		if(!c) {
			Response(RESPONSE_STATUS_BadRequest);
			return;
		}

		try {
			c->Setup();
			c->Start();
		}
		catch(std::exception& e) {
			Engine::DebugStream << "CRequestConnect::DoRequest - " << e.what() << '\n';
		}
	}
};

#endif //__CLIENT_REQUEST_CONNECT_included